1761C - Set Construction - CodeForces Solution


constructive algorithms dfs and similar graphs greedy

Please click on ads to support us..

Python Code:

import sys
FILE = False  
if FILE:
    sys.stdin = open('input.txt', 'r')
    sys.stdout = open('output.txt', 'w')
 
def get_int():
    return int(sys.stdin.readline())
 
def get_string():
    return sys.stdin.readline().strip()
 
test_cases= get_int()
for test in range(test_cases):
    n = get_int()
    matrix = [""] * (n + 1)
    sets = [set() for i in range(n+1)] 
    for i in range(1,n + 1):
        input = get_string()
        matrix[i] = " " + input
        for j in range(1,n+1):
            if matrix[i][j] == "0" and i!= j:
                continue
            sets[j].add(i)
    for i in range(1,n+1):
        print(len(sets[i]), *sets[i])
        

C++ Code:


#include <bits/stdc++.h>
#include <string.h>
#include <string>
#include <algorithm>

using namespace std;


#define ll long long
#define FIO ios::sync_with_stdio(false),cout.tie(0),cin.tie(0);

#define lp(i,n) for(ll i=0;i<n;i++)
#define lp1(i,n) for(ll i=1;i<=n;i++)

#define sz(x) sizeof(x)/sizeof(x[0])
#define szvi(vec) vec.size()

#define vi vector<int>
#define vvc vector<vector<char>>
#define vvi vector<vector<int>>
#define vll vector<ll>

#define pb(x) push_back(x)
queue<ll>perm;

const int mod = 32768;

const int N=10e2+7;
vector<int>adj[N];
vector<int>vis(N);
vi parent(N);
int n;
set<int>subset[N];
#define f(i,s,e) for(int i=s;i<e;++i)
#define feq(i,s,e) for(int i=s;i<=e;++i)

ll ans=0;
int cnt;

void dfs(int v){
    vis[v]=1;
    for(auto a:adj[v]){
        if(!vis[a]){
            dfs(a);
        }
        for (auto j = subset[a].begin(); j != subset[a].end(); j++)
            subset[v].insert((*j));
    }
    subset[v].insert(cnt++);
}

void solve(){
    cnt=1;
    cin>>n;
    char ch;
    
    parent=vi(n+1,0);
    vis=vi(n+1,0);

    f(i,1,n+1){
        adj[i].clear();
        subset[i].clear();
    }

    f(i,1,n+1){
        f(j,1,n+1){
            cin>>ch;
            if(ch=='1'){
                adj[j].pb(i);
                parent[i]++;
            }
        }
    }

    feq(i,1,n)
        if(!parent[i])
            dfs(i);

    feq(i,1,n){
        cout<<subset[i].size()<<" ";
        for (auto j = subset[i].begin(); j != subset[i].end(); j++)
            cout << (*j) << " ";

        cout<<endl;
    }

    return;
}

int main()
{
    FIO

    ll tc=1;
    cin>>tc;
    while(tc--){
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

462B - Appleman and Card Game
1560C - Infinity Table
1605C - Dominant Character
1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles
495A - Digital Counter
796A - Buying A House
67A - Partial Teacher
116A - Tram
1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home